﻿using Sys.Common;
using Sys.Core.Tour;
using Sys.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Sys.Api.Tour
{
    public class MeetingApi : BaseApi
    {
        public string Grid(int rows, int page, string search, string sort, string order)
        {
            var entities = MeetingBll.I.Entities;

            if (!string.IsNullOrEmpty(search))
            {
                entities = entities.Where(m => m.Name.Contains(search));
            }

            var list = MeetingBll.I.EntitiesPage(entities, rows, page, sort, order);

            var select = list.Select(m => new
            {
                ID = m.ID,
                Name = m.Name,
                Price = m.Price,
                Time = m.Time,
                Address = m.Address,
                InsertTime = m.InsertTime,
                UpdateTime = m.UpdateTime
            });

            return GetGrid(entities.Count(), select.ToList());
        }

        public string Save(TourMeeting model, string Images)
        {
            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                model.InsertTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;

                result.Success = MeetingBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                model.UpdateTime = DateTime.Now;

                result.Success = MeetingBll.I.Update(model) > 0;
            }

            if (result.Success)
            {
                var list = JsonHelper.Deserialize<List<TourImage>>(Images);

                ImageBll.I.SetImages("Meeting", model.ID, list);
            }
            else { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = MeetingBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string Upload()
        {
            Hashtable hash = KindHelper.ImgUpload();

            if (Convert.ToInt32(hash["error"]) == 0)
            {
                HttpContext context = HttpContext.Current;

                string filename = hash["url"].ToString();

                string temppath = context.Server.MapPath("~/userfiles/temp/" + filename);
                string filepath = context.Server.MapPath("~/userfiles/meeting/default/");
                string bigpath = context.Server.MapPath("~/userfiles/meeting/big/");
                string homepath = context.Server.MapPath("~/userfiles/meeting/home/");

                if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); }
                if (!Directory.Exists(bigpath)) { Directory.CreateDirectory(bigpath); }
                if (!Directory.Exists(homepath)) { Directory.CreateDirectory(homepath); }

                ThumNail.MakeThumNail(temppath, filepath + filename, 255, 153, TnType.Cut);
                ThumNail.MakeThumNail(temppath, bigpath + filename, 800, 480, TnType.Cut);
                ThumNail.MakeThumNail(temppath, homepath + filename, 246, 178, TnType.Cut);

                System.IO.File.Delete(temppath);

                hash["url"] = "../userfiles/meeting/default/" + filename;
            }

            return JsonHelper.ToJson(hash);
        }
    }
}
